package com.dfiles.view.activity;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.dfiles.utils.Task;
import com.dfiles.utils.ThreadWorker;

/**
 * 
 * @Description: Main Activity
 */
public class BaseActivity extends Activity {
	public static final String TAG = "BaseActivity";
	private boolean isDestory;
	private static BaseActivity runningActivity;
	private View rootView;
	private ActivityStack stack = ActivityStack.getInstanse();

	public static BaseActivity getRunningActivity() {
		return runningActivity;
	}

	public static void setRunningActivity(BaseActivity activity) {
		runningActivity = activity;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		stack.addActivity(this);
	}

	public View getRootView() {
		return rootView;
	}

	@Override
	protected void onResume() {
		super.onResume();
		runningActivity = this;
	}

	public void reload() {
		Intent intent = getIntent();
		overridePendingTransition(0, 0);
		intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		finish();
		overridePendingTransition(0, 0);
		startActivity(intent);
	}

	@Override
	public void onDestroy() {
		isDestory = true;
		super.onDestroy();
	}

	public boolean onHomeClick() {
		finish();
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if (onBackClick())
				return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public boolean onBackClick() {
		finish();
		return true;
	}

	@Override
	public void finish() {
		setMDestory(true);
		super.finish();
		stack.removeActivity(this);
	}

	public boolean mIsDestoryed() {
		return isDestory;
	}

	public void setMDestory(boolean destory) {
		this.isDestory = destory;
	}

	// public void setSystemBarTintManager(SystemBarTintManager
	// systemBarTintManager) {
	// this.systemBarTintManager = systemBarTintManager;
	// }
	//
	// public SystemBarTintManager getSystemBarTintManager() {
	// return systemBarTintManager;
	// }

	public interface IAcUnusedDoubleClickedHandler {
		public boolean onAcUnusedDoubleClicked();

	}

	public void addTask(Task task) {
		ThreadWorker.execuse(true, task);
	}

	/**
	 * 
	 * @Description: 堆栈中是否存在某个activity
	 * @param cName全路径
	 * @return boolean
	 * @throws
	 */
	public boolean isExistActivity(String cName) {
		boolean isTop = false;
		ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
		if (cn.getClassName().contains(cName)) {
			isTop = true;
		}
		return isTop;
	}
}
